import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import {nanoid} from 'nanoid'

Vue.use(Vuex)

export default new Vuex.Store({
	actions:{
		async getNews(miniStore){
			try {
				let result = await axios.get('https://api.uixsj.cn/hitokoto/get?type=social')
				const obj = {id:nanoid(),title:result.data}
				miniStore.commit('SAVE_NEWS',obj)
			} catch (error) {
				alert(error)
			}
		}
	},
	mutations:{
		SAVE_NEWS(state,newsObj){
			state.newsList.unshift(newsObj)
		},
		ADD_PERSON(state,personObj){
			state.persons.unshift(personObj)
		}
	},
	state:{
		newsList:[
			{id:'87tasu7d',title:'震惊，两男子竟然在教室做出这种事！'},
			{id:'87tasu8d',title:'这十种食物，一定要少吃！！'},
			{id:'87tasu9d',title:'健身的10大妙招'}
		],
		persons:[
			{id:'y76taasdy',name:'小猪佩奇'},
			{id:'y76taasqw',name:'小羊苏西'},
			{id:'y76taasft',name:'灰太狼'}
		]
	}
})